package com.hurl.waguji.web.cx;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;

import com.hurl.library.util.DateUtils;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.base.dao.HibernateService;
import com.hurl.waguji.persistence.SjLxzdgBO;
import com.opensymphony.xwork2.ActionSupport;

public class LxzdgAction extends ActionSupport{
	Map<String,Object> queryResult = new HashMap<String,Object>();
	private int page = 1;
	private static final int pageSize = 10;
	
	public Map<String,Object> getQueryResult(){
		return this.queryResult;
	}
	
	public int getPage() {
		return page<1?1:page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String zhang(){
		return this.query("z");
		
	}
	public String die(){
		return this.query("d");
	}
	public String query(String lx){
		Date date = getLatestDay();
		HibernateDao dao = HibernateService.getDao();
		
		/* count */
		String sql2 = SqlReader.getSql("SQL12061602",new String[]{DateUtils.formatDate(date),lx});
		BigInteger count = (BigInteger)dao.queryBySQL(sql2).list().get(0);
		queryResult.put("count", count);
		
		/* list */
		String sql = SqlReader.getSql("SQL12061601", new String[]{DateUtils.formatDate(date),lx,String.valueOf(pageSize*(page-1)),String.valueOf(pageSize)});
		List list = dao.queryBySQL(sql).list();
		if(list!=null || !list.isEmpty()){
			List<Map<String,String>> lxzdgList = new ArrayList<Map<String,String>>();
			Map<String,String> map = null;
			Object[] row = null;
			for(int i=0;i<list.size();i++){
				row = (Object[])list.get(i);
				map = new HashMap<String,String>();
				map.put("gpdm", String.valueOf(row[0]));
				map.put("gpmc", String.valueOf(row[1]));
				map.put("rqmax", DateUtils.formatDate((Date)row[2]));
				map.put("rqmin", DateUtils.formatDate((Date)row[3]));
				map.put("rqcount", String.valueOf(row[4]));
				map.put("pricemin", String.valueOf(row[5]));
				map.put("pricemax", String.valueOf(row[6]));
				map.put("zdf", String.valueOf(row[7]));
				lxzdgList.add(map);
			}
			queryResult.put("data", lxzdgList);
		}
		return SUCCESS;
	}
	public String execute(){
		return SUCCESS;
	}
	private Date getLatestDay(){
		HibernateDao dao = HibernateService.getDao();
		SjLxzdgBO bo = (SjLxzdgBO)dao.createCriteria(SjLxzdgBO.class).addOrder(Order.desc("id.rqMax")).setMaxResults(1).list().get(0);
		return bo==null?null:bo.getId().getRqMax();
	}
}
